package com.rapida.example;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.rapida.net.Host;
import com.rapida.net.UDPClient;
import com.rapida.net.UDPProtocol;
import com.rapida.theme.ThemeActivity;

import java.net.DatagramPacket;

public class ExampleThemeActivity extends ThemeActivity implements OnItemClickListener, UDPClient.HostListener, UDPClient.UPDHandler {
	private ImageView imageView;
	private ListView listView;
	private UDPClient client = new UDPClient();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.theme);
		listView = (ListView) findViewById(R.id.list);
		listView.setOnItemClickListener(this);
		client.initUDPClient();
		client.registerHostListener(this);
		client.registerHandler(this);
		client.startHost();
		listView.setAdapter(new ArrayAdapter<Host>(this, android.R.layout.simple_list_item_1, client.getHosts()));
		imageView = (ImageView) findViewById(R.id.logoImg);
		imageView.setImageDrawable(getThemeResources().getDrawable(R.drawable.mz_btn_check_button_square_on));
	}

	@Override
	protected void onStart() {
		super.onStart();
		client.broadCastRequestHost();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		client.cleanUPDClient();
	}

	@Override
	public void OnThemeChanged(String newThemePackageName) {
		super.OnThemeChanged(newThemePackageName);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	}

	@Override
	public void newHost(Host host) {
		listView.setAdapter(new ArrayAdapter<Host>(this, android.R.layout.simple_list_item_1, client.getHosts()));
	}

	@Override
	public void removeHost(Host host) {
		listView.setAdapter(new ArrayAdapter<Host>(this, android.R.layout.simple_list_item_1, client.getHosts()));
	}

	@Override
	public void receive(String ip, DatagramPacket packet, byte[] de) {
	}
}
